/**
 * 作者: heweiping
 * 日期: 2025-04-28 11:33:03
 * 备注: 用户每日特惠充值抽奖
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.RaffleDraw;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.ActManager;
import com.motu.monstercity.server.game.logic.RewardItem;
import com.motu.monstercity.server.game.logic.RewardManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.List;

public class UserDailyRaffleDraw extends UserDailyRaffleDrawModel {
    public UserDailyRaffleDraw() {
        super();
    }

    public JsonObject insert() {
        putGroupId(AllParam.RAFFLE_ITEM_GROUP[0]);
        putResetTime(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        List<RaffleDraw> raffleDrawList = ActManager.getRaffleDrawList(groupId);
        if (raffleDrawList.isEmpty() || isFinishDraw()) {
            return;
        }
        int nextGroupId = getNextGroupId(groupId);
        // 当期全部抽完后且有下一期的时候 隔天才重置
        if (!TimeUtils.isToday(getResetTime()) && getPostListArray().size() >= raffleDrawList.size() && nextGroupId != groupId) {
            putGroupId(nextGroupId);
            putResetTime(TimeUtils.getCurTime());
            putPostList("");
            update();
        }
    }

    // 是否全部抽完
    public boolean isFinishDraw() { // TODO @HWP 联调时 需确认一下 全部抽完，是否还需要下发数据，客户端需要判断图标是否隐藏，以及一键礼包充值额外获得道具得展示
        List<RaffleDraw> raffleDrawList = ActManager.getRaffleDrawList(groupId);
        return groupId >= getMaxGroupId() && getPostListArray().size() >= raffleDrawList.size();
    }

    // 获取最大的groupId
    public int getMaxGroupId() {
        return getGroupIdSize() > 0 ? AllParam.RAFFLE_ITEM_GROUP[getGroupIdSize() - 1] : 0;
    }

    // 获取下一期的groupId
    public int getNextGroupId(int currentGroupId) {
        if (getGroupIdSize() == 0) {
            return currentGroupId;
        }
        for (int i = 0; i < getGroupIdSize(); i++) {
            if (currentGroupId == AllParam.RAFFLE_ITEM_GROUP[i] && i < getGroupIdSize() - 1) {
                return AllParam.RAFFLE_ITEM_GROUP[i + 1];
            }
        }
        return currentGroupId;
    }
    public int getGroupIdSize() {
        return AllParam.RAFFLE_ITEM_GROUP.length;
    }

    // 一键礼包购买，下发额外奖励
    public void extraReward(UserInfo userInfo,  Common.PBUserData.Builder pbUserData) {
        if (getDrawCardNum() >= AllParam.RAFFLE_ITEM_MAX || isFinishDraw()) { // 超过上限，或者已经抽完，转成成其他道具
            List<RewardItem> rewardItemList = CommonUtils.takeReawrdItemFromStr(AllParam.RAFFLE_ITEM_CONVERT);
            RewardManager.addReward(userInfo, rewardItemList, pbUserData, LogType.IAP_GIFT_BUY);// 奖励
            return;
        }
        List<RewardItem> rewardItemList = CommonUtils.takeReawrdItemFromStr(AllParam.RAFFLE_ITEM_EXTRA);
        RewardManager.addReward(userInfo, rewardItemList, pbUserData, LogType.IAP_GIFT_BUY);// 奖励
        long addNum = rewardItemList.stream()
                .filter(rewardItem -> rewardItem.getItemId() == ItemId.TYPE_ACT_DAILY_RAFFLE_DRAW_ITEM)
                .mapToLong(RewardItem::getItemNum)  // 改为提取道具数量
                .sum();;
        addDrawCardNum((int) addNum);
        update();
        ProtoDataUtils.updatePBUserData(pbUserData, this);
    }
}
